package com.ybbase.framework.controller.system;
import com.ybbase.framework.common.constant.CommonResultCode;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RestController;
import com.ybbase.framework.base.controller.BaseController;
import com.ybbase.framework.service.system.AnnexService;
import com.ybbase.framework.model.po.system.Annex;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import com.ybbase.framework.base.model.ResponseBean;

/**
 * 系统附件 前端控制器
 * @author cg
 * @since 2019-11-26
 * 云平台2.0
 */
@RestController
@RequestMapping(value = "/annex", produces = {"application/json;charset=UTF-8"})
@Api("系统附件")
public class AnnexController extends BaseController {
    private static final Logger LOGGER = LogManager.getLogger(AnnexController.class);

    @Autowired
    private AnnexService targetService;


    /**
     * 系统附件分页查询
     * @param pageIndex 页数
     * @param step 每页数量
     * @return ResponseBean
     * @author cg
     * @date 2019-11-26
     * @since cloud2.0
    */
    @GetMapping("/list")
    @ApiOperation(value = "系统附件分页查询", notes = "系统附件分页查询", response = ResponseBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "rows", value = "每页行数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "annex", value = "系统附件", dataType = "Annex对象")
    })
    public ResponseBean findListByPage(@RequestParam(name = "page", defaultValue = "1") int pageIndex,
            @RequestParam(name = "rows", defaultValue = "20") int step,Annex annex){
        try{
            Wrapper<Annex> wrapper = new QueryWrapper<Annex>();
            Page<Annex> page = new Page(pageIndex,step);
            targetService.page(page,wrapper);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统附件分页查询"+CommonResultCode.SUCCESS.getMsg(),page);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("系统附件分页查询异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统附件分页查询"+CommonResultCode.FAIL.getMsg());
        }
    }


    /**
     * 系统附件获取全部数据
     * @return ResponseBean
     * @author cg
     * @date 2019-11-26
     * @since cloud2.0
     */
    @GetMapping("/all")
    @ApiOperation(value = "系统附件获取全部数据", notes = "系统附件获取全部数据")
    public ResponseBean findAll(){
        try{
            List<Annex> models = targetService.list();
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统附件获取全部数据"+CommonResultCode.SUCCESS.getMsg(),models);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("系统附件获取全部数据异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统附件获取全部数据"+CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统附件根据ID查找数据
     * @param id 唯一编码
     * @return ResponseBean
     * @author cg
     * @date 2019-11-26
     * @since cloud2.0
     */
    @GetMapping("/find")
    @ApiOperation(value = "系统附件根据ID查找数据", notes = "系统附件根据ID查找数据")
    @ApiImplicitParam(required = true, name = "id", value = "唯一编码", dataType = "Long")
    public ResponseBean find(@RequestParam("id") Long id){
        try{
            Annex annex = targetService.getById(id);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统附件根据ID查找数据"+CommonResultCode.SUCCESS.getMsg(),annex);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("系统附件根据ID查找数据异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统附件根据ID查找数据"+CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统附件保存数据
     * @param annex  系统附件
     * @return ResponseBean
     * @author cg
     * @date 2019-11-26
     * @since cloud2.0
     */
    @PostMapping("/save")
    @ApiOperation(value = "系统附件保存数据", notes = "系统附件保存数据")
    @ApiImplicitParam(required = true, name = "annex", value = "系统附件", dataType = "Annex对象")
    public ResponseBean save(@RequestBody Annex annex){
        try{
            targetService.save(annex);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统用户表保存数据"+CommonResultCode.SUCCESS.getMsg(),annex);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("系统附件保存数据：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统附件保存数据"+CommonResultCode.FAIL.getMsg());
        }
    }
    /**
     * 系统附件更新数据
     * @param annex  系统附件
     * @return ResponseBean
     * @author cg
     * @date 2019-11-26
     * @since cloud2.0
     */
    @PutMapping("/update")
    @ApiOperation(value = "系统附件更新数据", notes = "系统附件更新数据")
    @ApiImplicitParam(required = true, name = "annex", value = "系统附件", dataType = "Annex对象")
    public ResponseBean update(@RequestBody Annex annex){
        try{
            targetService.updateById(annex);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统附件更新数据"+CommonResultCode.SUCCESS.getMsg(),annex);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("系统附件更新数据：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统附件更新数据"+CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统附件删除数据
     * @param ids 批量删除数据
     * @return ResponseBean
     * @author cg
     * @date 2019-11-26
     * @since cloud2.0
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "系统附件删除数据", notes = "系统附件删除数据")
    @ApiImplicitParam(required = true, name = "ids", value = "批量删除数据", dataType = "List<Long>")
    public ResponseBean del(@RequestParam("ids") List<Long> ids){
        try{
            targetService.removeByIds(ids);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统附件删除数据"+CommonResultCode.FAIL.getMsg());
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("系统附件删除数据：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统附件删除数据"+CommonResultCode.FAIL.getMsg());
        }
    }


}
